<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 会员
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\Controller;
use think\Db;

class User extends Controller
{
  public function lst()
  {
    $user = Db::name('user')->order('id desc')->paginate(20);
    $this->assign('user',$user);
    return view();
  }

  public function edit()
  {
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if($id > 0) {
      //是修改操作
      if(request()->isPost()) {

        $post = input('post.');
        if(empty($post['vip'])) {
          $post['vip'] = 0;
        } else {
            $post['vip'] = $post['vip'];
        }
        $post['end_time'] = strtotime($post['end_time']);
        if(Db::name('user')->where('id',$id)->update($post)){
          $this->success('修改成功','user/lst');
        } else {
          $this->error('修改失败');
        }
      } else {
        //非提交操作
        $info = Db::name('user')->where('id',$id)->find();
        if(!empty($info)) {
          $this->assign('info',$info);
          return $this->fetch();
        } else {
          $this->error('id不正确');
        }
      }
    } else {
      $this->error('id错误');
    }
  }

  public function del()
  {
    if($this->request->isAjax()) {
      $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      if(false == Db::name('user')->where('id',$id)->delete()) {
        return $this->error('删除失败');
      } else {
        return $this->success('删除成功','user/lst');
      }
    }
  }
  
}
?>